<?php

namespace app\common\model;

use think\Cache;
/**
 * 统计模型
 */
class Statistics extends Base{


	/**
      * paynum [充值笔数]
      *
	  * @param type $sourceid [来源 1：cps 2：APP]
	  * @param type $model [1:小说,2:漫画]
	  * @param type $is_kouliang [是否扣量]
      * @param type $startData [开始时间]
      * @param type $endData   [结束时间]
      * @param type $type      [订单类型]
      * @param type $status    [订单状态]
      * @param type $recharge_type      [充值方式 1：直接充值 2：引导充值]
      * @return type
    **/
	public function paynum($wxmpid,$sourceid,$model='1',$is_kouliang=0,$startData,$endData,$type=0,$status=0,$articleid=0,$recharge_type=0,$agentid=0) {
		
		if($agentid){
			$map['agentid'] = $agentid;
		}else{
			//下线统计需要获取平台对应渠道用户
			if(strpos(input('server.PATH_INFO'),"agentlog")!==false || strpos(input('server.PATH_INFO'),"agentuserlog")!==false){
				
				if(session('user_auth.group')==999){
					$map['agentid'] =$this->agentUser(session('user_auth.uid'));
				}else{
					$map['agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
					$map['wxmpid'] = $wxmpid;
				}
			}else{
				if(session('user_auth.group')!=999){
					$map['agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
					$map['wxmpid'] = $wxmpid;
				}
			}
			
		}
		
		if($startData && $endData){
			$map['create_time'] =  array(array('gt',$startData),array('lt',$endData)) ;
		}
		if($sourceid){
			$map['sourceid'] = $sourceid;
		}
	
		//修改普通包括平台默认充值、活动充值、自定义充值
		if($type){
			if($type==1){
				$map['type'] = array('IN',"1,3,4");
			}else{
				$map['type'] = $type;
			}
		}
		

		if($recharge_type){
			$map['recharge_type'] = $recharge_type;
		}
		if($articleid){
			$map['articleid'] = $articleid;
		}
		
		if(strpos($model,",")!==false){
			$map['model'] = array('IN',$model);
		}else{
			$map['model'] = $model;
		}

		if($is_kouliang){
			$map['is_kouliang'] = 1;
		}else{
			$map['is_kouliang'] = 0;
		}
		$map['status'] = $status;
		
		if($endData<strtotime(date("Y-m-d"))){
			$cachename = 'statistics'.md5(serialize($map));
			if(Cache::get($cachename)){
				$total = Cache::get($cachename);
			}else{
				$total = db('order')->where($map)->count();
				Cache::set($cachename,$total);
			}
		}else{
			$total = db('order')->where($map)->count();
		}
		return $total;
	}

	/**
      * paySum [充值金额]
      *
	  * @param type $sourceid [来源 1：cps 2：APP]
	  * @param type $model [1:小说,2:漫画]
	  * @param type $is_kouliang [是否扣量]
      * @param type $startData [开始时间]
      * @param type $endData   [结束时间]
	  * @param type $type   [订单类型]
      * @param type $recharge_type      [充值方式 1：直接充值 2：引导充值]
	  * @param type $agentid      渠道商/代理商ID
      *
      * @return type
    **/
	public function paySum($wxmpid,$sourceid,$model,$is_kouliang=0,$startData,$endData,$type=0,$articleid=0,$recharge_type=0,$agentid=0) {

		$map['status'] = 1;
		if($agentid){
			$map['agentid'] = $agentid;
		}else{
			//下线统计需要获取平台对应渠道用户
			if(strpos(input('server.PATH_INFO'),"agentlog")!==false || strpos(input('server.PATH_INFO'),"agentuserlog")!==false){
				
				if(session('user_auth.group')==999){
					$map['agentid'] =$this->agentUser(session('user_auth.uid'));
				}else{
					$map['agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
					$map['wxmpid'] = $wxmpid;
				}
			}else{
				if(session('user_auth.group')!=999){
					$map['agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
					$map['wxmpid'] = $wxmpid;
				}
			}
		}
		
		
		if($startData && $endData){
			$map['create_time'] =  array(array('egt',$startData),array('elt',$endData)) ;
		}

		if($sourceid){
			$map['sourceid'] = $sourceid;
		}

		if($recharge_type){
			$map['recharge_type'] = $recharge_type;
		}
		
		//修改普通包括平台默认充值、活动充值、自定义充值
		if($type){
			if($type==1){
				$map['type'] = array('IN',"1,3,4");
			}else{
				$map['type'] = $type;
			}
		}

		if(strpos($model,",")!==false){
			$map['model'] = array('IN',$model);
		}else{
			$map['model'] = $model;
		}
		if($is_kouliang){
			$map['is_kouliang'] = $is_kouliang;
		}else{
			$map['is_kouliang'] = 0;
		}
		if($articleid){
			$map['articleid'] = $articleid;
		}
		
		if($endData && $endData<strtotime(date("Y-m-d"))){
			$cachename = 'paySum'.md5(serialize($map));
			if(Cache::get($cachename)){
				$sum = Cache::get($cachename);
			}else{
				$sum = db('order')->where($map)->cache(true,600)->sum('money');
				Cache::set($cachename,$sum);
			}
		}else{
			
			$sum = db('order')->where($map)->cache(true,600)->sum('money');
		}
		
		//$sum = db('order')->where($map)->cache(true,600)->sum('money');
		return $sum?$sum:0;
	}


	/**
      * payUser [下单人数]
      *
	  * @param type $model [模型]
	  * @param type $is_kouliang [是否口量]
      * @param type $startData [开始时间]
      * @param type $endData   [结束时间]
	  * @param type $status   [订单状态] 1：完成 0:下单
      *
      * @return type
    **/
	protected function payUser($model='1',$is_kouliang=0,$startData,$endData,$status=0) {
		$map = [];
	
		if($status){
			$map['status'] = $status;
		}
		
		if($is_kouliang){
			$map['is_kouliang'] = $is_kouliang;
		}else{
			$map['is_kouliang'] = 0;
		}
		

		$map['model'] = $model;//1:小说,2:漫画
		
		if($startData && $endData){
			$map['create_time'] =  array(array('gt',$startData),array('lt',$endData)) ;
		}
	
		if(session('user_auth.group')!=999){
			$map['agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
			$map['wxmpid'] = session('wxmpid');
		}

		$total = db('order')->where($map)->cache(true,600)->group('uid')->count();

		return $total;
	}


	/**
      * newUser [新增用户]
      *
      * @param type $startData [开始时间]
      * @param type $endData   [desc]
      * @param type $sex       [性别]
      * @param type $attention [已关注]
      * @param type $pay       [已付费]
      * @param type $is_guide       [引导注册用户]
	  * @param type $is_guideattention [引导关注用户]
		
      * @return type
    **/
	public function newUser($wxmpid,$sourceid,$startData,$endData,$sex=null,$attention=0,$pay=0,$is_guide=null,$is_guideattention=null,$agentid=0) {
		$map['status'] = 1;
		
		if($agentid){
			$map['agentid'] = $agentid;
		}else{
			//下线统计需要获取平台对应渠道用户
			if(strpos(input('server.PATH_INFO'),"agentlog")!==false || strpos(input('server.PATH_INFO'),"agentuserlog")!==false){
				
				if(session('user_auth.group')==999){
					$map['agentid'] =$this->agentUser(session('user_auth.uid'));
				}else{
					$map['agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
					$map['wxmpid'] = $wxmpid;
				}
			}else{
				if(session('user_auth.group')!=999){
					$map['agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
					$map['wxmpid'] = $wxmpid;
				}
			}
		}
		

		if($startData && $endData){
			$map['reg_time'] =  array(array('gt',$startData),array('lt',$endData)) ;
		}
		
		if($sourceid){
			$map['sourceid'] = $sourceid;
		}
		if(isset($sex)){
			$map['sex'] = $sex;
		}

		if(isset($is_guide)){
			$map['is_guide'] = $is_guide;
		}

		if(isset($is_guideattention)){
			$map['is_guideattention'] = $is_guideattention;
		}

		if($attention){
			$map['is_attention'] = $attention;
		}
		if($pay){
			$map['pay'] = array('egt',$pay);
		}
		$map['group'] =99;
		
		if($endData<strtotime(date("Y-m-d"))){
			$cachename = 'newUser'.md5(serialize($map));
			if(Cache::get($cachename)){
				$sum = Cache::get($cachename);
			}else{
				$sum = db('member')->where($map)->count('uid');
				Cache::set($cachename,$sum);
			}
		}else{
			$sum = db('member')->where($map)->count('uid');
		}
		//$sum = db('member')->where($map)->count();
		return $sum;

	}
	
	/**
      * newUserpay [新用户充值金额]
      *
	  * @param type $wxmpid   [公众号ID]
	  * @param type $is_kouliang   [是否口量]
	  * @param type $startData [开始时间]
	  * @param type $endData [结束时间]
	  * @param type $attention [是否关注]
      * @return type
    **/
	public function newUserpay($wxmpid,$is_kouliang=0,$startData,$endData,$attention=0){
		
		
		$map['o.status'] = 1;
		
		if($is_kouliang){
			$map['o.is_kouliang'] = $is_kouliang;
		}else{
			$map['o.is_kouliang'] = 0;
		}

		$map['o.model'] = 1;//1:小说,2:漫画
		
		

		if(session('user_auth.group')!=999){
			$map['o.agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
			$map['o.wxmpid'] = $wxmpid;
		}

		if($attention){
			$map['m.is_attention'] = $attention;
		}

		if($startData && $endData){
			$map['m.reg_time'] =  array(array('gt',$startData),array('lt',$endData)) ;

			$map['o.create_time'] =  array(array('gt',$startData),array('lt',$endData)) ;
		}
		
		if($endData<strtotime(date("Y-m-d"))){
			$cachename = 'newUserpay'.md5(serialize($map));
			if(Cache::get($cachename)){
				$sum = Cache::get($cachename);
			}else{
				$sum = db('order')->alias('o')->join('__MEMBER__ m','m.uid = o.uid','left')->where($map)->cache(true,600)->sum('o.money');
				Cache::set($cachename,$sum);
			}
		}else{
			$sum = db('order')->alias('o')->join('__MEMBER__ m','m.uid = o.uid','left')->where($map)->cache(true,600)->sum('o.money');
		}

		return $sum?$sum:0;
	}
	

	/**
      * newUserpay [新用户充值人数]
      *
	  * @param type $wxmpid   [公众号ID]
	  * @param type $is_kouliang   [是否口量]
	  * @param type $startData [开始时间]
	  * @param type $endData [结束时间]
	  * @param type $attention [是否关注]
      * @return type
    **/
	public function newUserpaynum($wxmpid,$is_kouliang=0,$startData,$endData,$attention=0){
		
		
		$map['o.status'] = 1;
		
		if($is_kouliang){
			$map['o.is_kouliang'] = $is_kouliang;
		}else{
			$map['o.is_kouliang'] = 0;
		}

		$map['o.model'] = 1;//1:小说,2:漫画
		
		

		if(session('user_auth.group')!=999){
			$map['o.agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
			$map['o.wxmpid'] = $wxmpid;
		}

		if($attention){
			$map['m.is_attention'] = $attention;
		}

		if($startData && $endData){
			$map['m.reg_time'] =  array(array('gt',$startData),array('lt',$endData)) ;

			$map['o.create_time'] =  array(array('gt',$startData),array('lt',$endData)) ;
		}
		
		if($endData<strtotime(date("Y-m-d"))){
			$cachename = 'newUserpaynum'.md5(serialize($map));
			if(Cache::get($cachename)){
				$count = Cache::get($cachename);
			}else{
				$count = db('order')->alias('o')->join('__MEMBER__ m','m.uid = o.uid','left')->where($map)->cache(true,600)->count();
				Cache::set($cachename,$count);
			}
		}else{
			$count = db('order')->alias('o')->join('__MEMBER__ m','m.uid = o.uid','left')->where($map)->cache(true,600)->count();
		}

		return $count?$count:0;
	}
	

	/**
      * dataSection [今天/昨天/月/时间区间]
      *
      * @return type
    **/
	public function dataSection() {

		$result['today']['Start'] = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
        $result['today']['End'] = mktime(23, 59, 59, date("m"), date("d"), date("Y"));

		$result['yesterday']['Start'] = mktime(0, 0, 0, date('m'), date('d')-1, date('Y'));
        $result['yesterday']['End']   = mktime(0, 0, 0, date('m'), date('d'), date('Y'))-1;

		$result['month']['Start'] =mktime(0, 0, 0, date("m"), 1, date("Y"));
        $result['month']['End'] =mktime(23, 59, 59, date("m"), date("t"), date("Y"));

		$result['prevmonth']['Start'] =mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
        $result['prevmonth']['End'] =mktime(23, 59, 59, date('m') - 1, date('t', $result['prevmonth']['Start']), date('Y'));
		
		return $result;
	}
}